//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
	detail:{
		content:"正在获取中...",
		title:"获取中...",
		like_num:0,
		ColorList: app.globalData.ColorList
	},
  },

  onLoad: function () {
    wx.showLoading({
      title:"加载中..."
    })
	let that = this
    app.httpGet('mail/getPublicLetter',{
       user_id:app.globalData.dbInfo.id
    },function(res){
		wx.hideLoading()
		if(res.status==1){
			that.setData({
				detail:res.detail
			})
		}else if(res.status==0){
			wx.showToast({
				title:res.msg,
				icon:"none"
			})
		}else{
			wx.showToast({
				title:"数据库出错，请联系开发者！",
				icon:"none"
			})			
		}
		
	})
	  
	  
  },
  nextLetter(){
	  this.onLoad()
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0,
		duration:200
      })
    } 
    
  },
  likeLetter() {
  	let that = this

    var detail = that.data.detail
  	detail.like_num ++
    detail.is_liked = true
  	that.setData({
  		detail: detail
  	})

  	app.httpGet('mail/likeThisLetter', {
  		letter_id: that.data.detail.id,
  		user_id: app.globalData.dbInfo.id
  	}, function(res) {
  		if (res.status == 1) {
  			var detail = that.data.detail
  			detail.like_num = res.like_num
        detail.is_liked = true
  			that.setData({
  				detail: detail
  			})
  		} else if (res.status == 0) {
  			wx.showToast({
  				title: res.msg,
  				icon: "none"
  			})
  		} else {
  			wx.showToast({
  				title: "服务器异常，请上报开发者！",
  				icon: "none"
  			})
  		}
  	})
  },
  secondLike(){

  },
  onShareAppMessage: function () {
    let that = this

    qq.showShareMenu({
      showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
    })
  
    var title = "这封信写得超棒，快来围观——" + that.data.detail.title;
    console.log(title)
  
    return {
        title:title,
        desc: '写给未来的信',
        // imageUrl: '/images/logo.png',        
		path: '/pages/public/private_share/private_share?user_id=public' + "&letter_id=" + that.data.detail.id
    }
  
  }

})
